package com.itheima.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Slf4j
@Aspect // 表示这是一个切面类
@Component
public class RecordTimeAspect {

    @Around("@annotation(com.itheima.annotation.Log)")
//    @Around("execution(* com.itheima.service.impl.DeptServiceImpl.*(..) )") //这个表达式就是来指定针对于哪些方法编程
    public Object recordTime(ProceedingJoinPoint pjp) throws Throwable {
        //1. 记录方法运行的开始时间
        long start = System.currentTimeMillis();
        //2. 运行原始方法
        Object result = pjp.proceed();

        //3. 记录方法运行结束时间, 计算耗时
        long end = System.currentTimeMillis();
        log.info("方法运行耗时: {}", end - start);
        return result;
    }


    public Object recordTime2(ProceedingJoinPoint pjp) throws Throwable {
        String name = pjp.getTarget().getClass().getName();
        log.info("name {}",name);
        String methodName = pjp.getSignature().getName();
        log.info("methodName {}",methodName);
        Object[] args = pjp.getArgs();
        log.info("args {}", Arrays.toString(args));
        Object result = pjp.proceed(args);
        return result;
    }

}
